package com.asiainfo.cms.system.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.asiainfo.cms.common.constant.CommonConstant;
import com.asiainfo.cms.common.result.Result;
import com.asiainfo.cms.common.security.jwt.JwtToken;
import com.asiainfo.cms.common.security.userdetails.MyUserDetails;
import com.asiainfo.cms.common.service.SysPermissionService;
import com.asiainfo.cms.common.util.SecurityUtils;
import com.asiainfo.cms.common.vo.LoginUser;
import com.asiainfo.cms.system.entity.SysMenu;
import com.asiainfo.cms.system.entity.SysUser;
import com.asiainfo.cms.system.service.ISysMenuService;
import com.asiainfo.cms.system.service.ISysUserService;
import com.asiainfo.cms.system.vo.SysMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController {

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private JwtToken jwtToken;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public Result login(@RequestBody JSONObject loginBody) {
        String username = loginBody.getString("username");
        String password = loginBody.getString("password");
        MyUserDetails myUserDetails = (MyUserDetails) authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(username, password)).getPrincipal();
        String token = jwtToken.generateToken(myUserDetails);
        Map<String, Object> data = new HashMap<>();
        data.put("details", myUserDetails.getLoginUser());
        data.put(CommonConstant.TOKEN, token);
        return Result.success(data);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public Result getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = userService.selectUserByUserName(loginUser.getUsername());
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(loginUser.getUserId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(loginUser.getUserId());

        JSONObject data = new JSONObject();
        data.put("user", user);
        data.put("roles", roles);
        data.put("permissions", permissions);
        data.put("portalId", 4);
        return Result.success(data);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public Result getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenuVO> menus = menuService.selectMenuTreeByUserId(userId);
        return Result.success(menuService.buildMenus(menus));
    }
}
